package org.jeecg.modules.third.jg.jss.report.cfcvehicleinfo.service;

import cn.hutool.core.util.StrUtil;
import org.jeecg.modules.third.jg.jss.report.cfcvehicleinfo.entity.CfcVehicleinfo;
import org.jeecg.modules.third.jg.jss.report.cfcvehicleinfo.mapper.CfcVehicleinfoMapper;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 车辆基础信息
 * @Description: CFC_VEHICLEINFO
 * @Author: jeecg-boot
 * @Date:   2021-11-25
 * @Version: V1.0
 */
@Service
public class CfcVehicleinfoService {

    @Resource
    private CfcVehicleinfoMapper vehicleinfoMapper;

    @Resource
    private IHyVehicleService vehicleService;

    public String getEnterpriseCode() {
        return enterpriseCode;
    }

    public void setEnterpriseCode(String enterpriseCode) {
        this.enterpriseCode = enterpriseCode;
    }

    private String enterpriseCode;

    public CfcVehicleinfo selecVehicleinfo(String vehicleid) {
        return vehicleinfoMapper.selecVehicleinfo( vehicleid );
    }


    public CfcVehicleinfo getVehicleinfoByNumber(String vehiclenumber) {
        return vehicleinfoMapper.getVehicleinfoByNumber( vehiclenumber );
    }


    public CfcVehicleinfo getVehicleinfoByCarrierid(String carrierid) {
        return vehicleinfoMapper.getVehicleinfoByCarrierid( carrierid );
    }



    public boolean addVehicleinfo( HyVehicle vehicle ) {

        CfcVehicleinfo cfcVehicleinfoDb = vehicleinfoMapper.getVehicleinfoByNumber( vehicle.getVehicleNumber() );
        if(cfcVehicleinfoDb != null){
            return false;
        }
        CfcVehicleinfo cfcVehicleinfo =  vehicleTOCfcVehicleinfo(vehicle);
        Integer res =  vehicleinfoMapper.insertSelective( cfcVehicleinfo );
        return res > 0 ? true : false ;
    }



    public int deleteVehicleinfo(String vehicleid) {
        return vehicleinfoMapper.deleteVehicleinfo( vehicleid );
    }

    public CfcVehicleinfo vehicleTOCfcVehicleinfo(HyVehicle vehicle){
        if (vehicle == null) {
            return null;
        }

        CfcVehicleinfo cfcVehicleinfo = new CfcVehicleinfo();
        cfcVehicleinfo.setPlatformcode(this.getEnterpriseCode());
        cfcVehicleinfo.setVehicleid(vehicle.getId());
        cfcVehicleinfo.setVehicleclassificationcode(vehicle.getVehicleType());
        cfcVehicleinfo.setLicenseplatetypecode("01");
        cfcVehicleinfo.setVehiclenumber(vehicle.getVehicleNumber());
        cfcVehicleinfo.setVehiclelicenseplatecolor(vehicle.getVehiclePlateColorCode());
        cfcVehicleinfo.setVehicleladenweight(vehicle.getGrossMass());
        cfcVehicleinfo.setVehicletonnage(vehicle.getVehicleTonnage());
        cfcVehicleinfo.setRoadtransportcertificatenumber(vehicle.getRoadTransportNumber());
        if (StrUtil.isNotEmpty(vehicle.getVehicleLong())) {
            cfcVehicleinfo.setVehiclelength(new BigDecimal(vehicle.getVehicleLong()).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        if (StrUtil.isNotEmpty(vehicle.getVehicleWidth())) {
            cfcVehicleinfo.setVehiclewidth(new BigDecimal(vehicle.getVehicleWidth()).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        if (StrUtil.isNotEmpty(vehicle.getVehicleHeight())) {
            cfcVehicleinfo.setVehicleheight(new BigDecimal(vehicle.getVehicleHeight()).divide(new BigDecimal(1000)).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        cfcVehicleinfo.setAxlenum("5");
        cfcVehicleinfo.setCarrierid(vehicle.getCarrierId());
        cfcVehicleinfo.setVehicleenergytype(vehicle.getVehicleEnergyType());
        cfcVehicleinfo.setOwner(vehicle.getOwner());
        cfcVehicleinfo.setUsercharacter(vehicle.getUseCharacter());
        cfcVehicleinfo.setVin(vehicle.getVin());
        cfcVehicleinfo.setIssuingorganizations(vehicle.getIssuingOrganizations());
        cfcVehicleinfo.setRegisterdate(vehicle.getRegisterDate());
        cfcVehicleinfo.setIssuedate(vehicle.getIssueDate());
        if (StrUtil.isNotEmpty(vehicle.getTrailerVehiclePlateNumber())) {
            cfcVehicleinfo.setTrailerlicenceno(vehicle.getTrailerVehiclePlateNumber());
        }

        return cfcVehicleinfo;
    }

}
